package gui2.g1;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class ChatGUI extends JFrame {

	private JLabel caption;

	private JLabel lblUsername;
	private JTextField fldUsername;
	private JButton btnStartChat;

	private JList chatList;
	private JTextField txtChat;
	private JButton btnSendText;

	public ChatGUI() {
		setTitle("Chat");
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		createWidgets();
		layoutWidgets();
		
		pack();
		setLocationRelativeTo(null);
	}

	private void createWidgets() {
		caption = new JLabel("CHAT");
		caption.setPreferredSize(new Dimension(500, 50));
		caption.setHorizontalAlignment(SwingConstants.CENTER);
		caption.setFont(caption.getFont().deriveFont(25f));

		lblUsername = new JLabel("Username:");
		fldUsername = new JTextField();
		fldUsername.setMaximumSize(new Dimension(220, 25));

		btnStartChat = new JButton("Start Chat!");

		chatList = new JList();
		chatList.setPreferredSize(new Dimension(300, 300));
		chatList.setBorder(BorderFactory.createLineBorder(Color.BLACK));

		txtChat = new JTextField();

		btnSendText = new JButton("Send");
	}

	private void layoutWidgets() {
		getContentPane().setLayout(new BorderLayout(5, 5));

		getContentPane().add(caption, BorderLayout.NORTH);

		JPanel leftPanel = new JPanel();
		leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));

		leftPanel.add(lblUsername);
		leftPanel.add(fldUsername);
		leftPanel.add(btnStartChat);

		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new GridBagLayout());

		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 2;
		c.gridheight = 1;
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1;
		c.weighty = 1;
		c.insets = new Insets(2, 2, 2, 2);
		centerPanel.add(chatList, c);

		c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 1;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 1;
		centerPanel.add(txtChat, c);

		c = new GridBagConstraints();
		c.gridx = 1;
		c.gridy = 1;
		c.gridwidth = 1;
		c.gridheight = 1;
		centerPanel.add(btnSendText, c);

		getContentPane().add(leftPanel, BorderLayout.WEST);
		getContentPane().add(centerPanel, BorderLayout.CENTER);
	}

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {

				new ChatGUI().setVisible(true);
			}
		});
	}

}
